<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>xhr_status</title>
  </head>
  <body>
    <script type="text/javascript">
      // 当xhr对象实例化后, readyState处于状态 0
      var xhr = new XMLHttpRequest();
      console.log('readyState at: ', xhr.readyState);
      xhr.onreadystatechange = function () {
        // 状态2、3、4都在事件处理函数中体现
        console.log('readyState at: ', xhr.readyState);
        // 因此status = 200、readyState = 4 意为xhr对象成功获取到服务器返回数据
        if (xhr.status === 200 && xhr.readyState === 4) {
          console.log(xhr.responseText);
        }
      };
      // 当xhr调用open方法后, readyState处于状态 1
      xhr.open('GET', 'http://localhost:3000/', true);
      xhr.send();
    </script>
  </body>
</html>
